package com.dm.hdfxxt.outmanage.service.impl;

import com.dm.hdfxxt.outmanage.mapper.OutMapper;
import com.dm.hdfxxt.outmanage.service.OutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor=Exception.class)
public class OutServiceImpl implements OutService {
    @Autowired
    private OutMapper outMapper;

    /**
     * @Description 查询所有该店面入库
     * @Author zhangxue
     * @Date 11:36 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public List<Map<String, Object>> queryAllInBound(Map<String, Object> param) {
        List<Map<String,Object>> ResultList = outMapper.queryAllInBound(param);
        return ResultList;
    }

    /**
         * @Description 保存出库信息
         * @Author zhangxue
         * @Date 11:19 2018/10/30
         * @Param
         * @retrun
         **/
    @Override
    public Map<String, Object> saveOutBound(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        //通过入库单据号查询库存数量
        String InNo = param.get("InNo").toString();
        Map<String,Object> InMap = outMapper.queryRestnumByInNo(param);
        int RestNumber = Integer.valueOf(InMap.get("RestNumber").toString());
        //得到出库数量
        int OutNumber = Integer.valueOf(param.get("OutNumber").toString());
        //获取单据类型
        String DocType = param.get("DocType").toString();
        //最终剩余数量
        int FinalRestNumber = 0;
        if("2".equals(DocType)){
            int i = outMapper.saveOutBound(param);
            if(i==1){
                FinalRestNumber = RestNumber+OutNumber;
                param.put("FinalRestNumber",FinalRestNumber);
                int j = outMapper.updateInBoundRestnum(param);
                if(j==1){
                    StatusMap.put("Status",1);//保存成功
                }else{
                    //如果不是影响一条，直接回滚
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    StatusMap.put("Status",0);//保存失败
                }
            }
        }else{
            if(OutNumber>RestNumber){
                StatusMap.put("Status",-2);//保存失败，库存不足
            }else{
                int i = outMapper.saveOutBound(param);
                if(i==1){
                    FinalRestNumber = RestNumber-OutNumber;
                    param.put("FinalRestNumber",FinalRestNumber);
                    int j = outMapper.updateInBoundRestnum(param);
                    if(j==1){
                        StatusMap.put("Status",1);//保存成功
                    }else{
                        //如果不是影响一条，直接回滚
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        StatusMap.put("Status",0);//保存失败
                    }
                }
            }
        }

        return StatusMap;
    }

    /**
     * @Description 查询所有该店面出库单信息
     * @Author zhangxue
     * @Date 11:36 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> queryAllOutBound(Map<String, Object> param) {
        List<Map<String,Object>> ResultList = outMapper.queryAllOutBound(param);
        for(int i=0;i<ResultList.size();i++){
            String CreateTimeStr = ResultList.get(i).get("CreateTime")!=null?ResultList.get(i).get("CreateTime").toString():null;
            ResultList.get(i).put("CreateTime",CreateTimeStr);
        }
        Map<String,Object> countMap = outMapper.queryAllOutBoundCount(param);
        int ckTotal = Integer.valueOf(countMap.get("total").toString());
        Map<String,Object> resultMap = new HashMap<String, Object>();
        resultMap.put("ResultList",ResultList);
        resultMap.put("ckTotal",ckTotal);
        return resultMap;
    }

    /**
     * @Description 编辑出库信息
     * @Author zhangxue
     * @Date 11:19 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> editDealer(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        return StatusMap;
    }

    /**
     * @Description 删除出库信息
     * @Author zhangxue
     * @Date 11:19 2018/10/30
     * @Param
     * @retrun
     **/
    @Override
    public Map<String, Object> delOutBound(Map<String, Object> param) {
        Map<String,Object> StatusMap = new HashMap<String, Object>();
        //修改入库单据剩余数量
        int i = outMapper.updateInNound(param);

        if(i==1){
            int j = outMapper.delOutBound(param);
            if(j==1){
                StatusMap.put("Status",1);//删除成功
            }else{
                //如果不是影响一条，直接回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                StatusMap.put("Status",0);//删除失败
            }
        }
        return StatusMap;
    }


}
